import {observable, makeAutoObservable, runInAction} from "mobx";

import type { GisMapStore, Earth3dVisProps } from "@/components/app/earth3dVis";
import { type RespDataType, getInitData } from "../api";
import { MainNetworkStore } from "./MainNetworkStore";
import { LocalNetworkStore } from "./LocalNetworkStore";

export type {
    MainNetworkStore,
    LocalNetworkStore
}

export class SimulationStore{
    mainNetworkStore: MainNetworkStore;
    localNetworkStore: LocalNetworkStore;

    gisMapStore: GisMapStore;
    loading: boolean = true;
    initData: RespDataType.InitData;

    constructor() {
        makeAutoObservable(this,{
            initData: observable.ref,
        })
    }

    // 监听地图加载完成
    onMapDidMount: Earth3dVisProps["onMapDidMount"] = (gisMapStore) =>{
        this.gisMapStore = gisMapStore;
        const { viewer } = this.gisMapStore.cesiumUtil;
        // 罗盘
        viewer.compass.enable = true;
        // // 放大缩小
        // viewer.zoomController.enable = true;
        // 比例尺
        viewer.distanceLegend.enable = true;
        // 位置信息
        viewer.locationBar.enable = true;

        this.fetchInitData().then(() => {
            this.mainNetworkStore = new MainNetworkStore(this);
            this.localNetworkStore = new LocalNetworkStore(this);
            this.mainNetworkStore.renderNetwork()
        });
    }

    /**
     * 获取初始化数据
     */
    private fetchInitData = () => {
        this.loading = true;
        return getInitData().then((resp) => runInAction(() => {
            this.initData = resp.data;
            this.loading = false;
        }))
    }

}